//
//  NetWork.m
//  TheProjectFrameWork
//
//  Created by maple on 16/6/4.
//  Copyright © 2016年 MapleDongSen. All rights reserved.
//

#import "NetWork.h"
#import "NetWorkType.h"
#import "NetWork+BodyAndCookie.h"
@implementation NetWork

/**
 *  POST请求
 *
 *  @param url        请求url
 *  @param data       参数
 *  @param block      <#block description#>
 *  @param errorblock <#errorblock description#>
 */
+ (void)postNetWorkWithUrl:(NSString *)url parame:(id)parame successBlock:(SuccessCallBack)dicback errorBlock:(ErrorCallBack)errorblock
{
    [NetWork setHttpCookie];
    [NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
    NSString * urlString = [url stringByReplacingOccurrencesOfString:@" " withString:@""];
    urlString = [NSString stringWithFormat:@"%@/%@",kRootUrl,urlString];
    AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [sessionManager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", nil]];
//    NSLog(@" header = %@", sessionManager.requestSerializer.HTTPRequestHeaders);
    [sessionManager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    [sessionManager POST:urlString parameters:parame success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [NetWork getHttpCookie];
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:responseObject
                              options:kNilOptions
                              error:NULL];
        if ([NetWork checkLoginWithJsonDic:json]) {
            if(dicback)
            {
                dicback(json);
            }
        } else {
            return ;
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(errorblock)
        {
            NSLog(@"error = %@", error);
            errorblock(error);
        }
    }];
#pragma clang diagnostic pop
}



+ (void)loadImageWithUrl:(NSString *)url ImageDownloaderProgressBlock:(ImageDownloaderProgressBlock)progressBlock
ImageCompletionWithFinishedBlock:(ImageCompletionWithFinishedBlock)completionBlock
{

    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:SDWebImageRetryFailed progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if(completionBlock)
        {
        completionBlock(image,error,finished,imageURL);
            
        }
    }];
}

+ (void)uploadImageWithUrl:(NSString *)url image:(UIImage *)image imageName:(NSString *)imageName successBlock:(SuccessCallBack)dicback errorBlock:(ErrorCallBack)errorblock{
    [NetWork setHttpCookie];
    [NSHTTPCookieStorage sharedHTTPCookieStorage].cookieAcceptPolicy = NSHTTPCookieAcceptPolicyAlways;
    NSString * urlString = [url stringByReplacingOccurrencesOfString:@" " withString:@""];
    urlString = [NSString stringWithFormat:@"%@/%@",kRootUrl,urlString];
    
    AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
    //    sessionManager
    sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
    sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [sessionManager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
    [sessionManager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSData *data = UIImageJPEGRepresentation(image, 1.0);
        NSLog(@"imageData = %@", data);
        [formData appendPartWithFileData :data name:imageName fileName:[NSString stringWithFormat:@"%@.jpg", imageName] mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [NetWork getHttpCookie];
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:responseObject
                              options:kNilOptions
                              error:NULL];
        if(dicback)
        {
            dicback(json);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(errorblock)
        {
            NSLog(@"error = %@", error);
            errorblock(error);
        }
    }];
}



@end
